﻿<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">


  <!--
  Templates used to create folder output according the desired mode
  -->

  <xsl:template match="folders">
    <xsl:param name="application" />
    <xsl:param name="server" />

    <xsl:call-template name="SwitchToManifestTemplate">
      <xsl:with-param name="application" select="$application" />
      <xsl:with-param name="server" select="$server" />
    </xsl:call-template>

    <!-- Browse folders to create Folder attribute -->
    <xsl:apply-templates select="./folder">
      <xsl:with-param name="application" select="$application" />
      <xsl:with-param name="server" select="$server" />
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="folder[@type='server']">
    <xsl:param name="application" />
    <xsl:param name="server" />

    <xsl:call-template name="SwitchToManifestTemplate">
      <xsl:with-param name="application" select="$application" />
    </xsl:call-template>

    <xsl:if test="@name">
      <xsl:attribute name="{@name}">
        <xsl:call-template name="EnsurePathEndTerminator">
          <xsl:with-param name="value" select="@value" />
          <xsl:with-param name="separator" select="'\'" />
        </xsl:call-template>
      </xsl:attribute>
    </xsl:if>

    <xsl:if test="@share">

      <xsl:variable name="shareServer">
        <xsl:choose>
          <xsl:when test="$server">
            <xsl:value-of select="concat('\\',$server/@name)"/>
            <xsl:value-of select="'\'" />
            <xsl:value-of select="@share"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'\\ERROR'" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      
      <xsl:attribute name="{concat('remote',@name)}">
        <xsl:call-template name="EnsurePathEndTerminator">
          <xsl:with-param name="value" select="$shareServer" />
          <xsl:with-param name="separator" select="'\'" />
        </xsl:call-template>
      </xsl:attribute>
    </xsl:if>

  </xsl:template>

  <xsl:template match="folder[@type='application']">
    <xsl:param name="application" />
    <xsl:param name="server" />

    <xsl:call-template name="SwitchToManifestTemplate">
      <xsl:with-param name="application" select="$application" />
    </xsl:call-template>

    <xsl:if test="@name">
      <xsl:attribute name="{@name}">
        <xsl:call-template name="CreateServerFolderPathForApplication">
          <xsl:with-param name="application" select="$application" />
          <xsl:with-param name="environment" select="$EnvironmentTarget" />
          <xsl:with-param name="folderPath" select="@value" />
        </xsl:call-template>
      </xsl:attribute>
    </xsl:if>

    <xsl:if test="@share">

      <xsl:variable name="shareServer">
        <xsl:choose>
          <xsl:when test="$server">
            <xsl:value-of select="concat('\\',$server/@name)"/>
            <xsl:value-of select="'\'" />
            <xsl:value-of select="@share"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="'\\ERROR'" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:attribute name="{concat('remote',@name)}">
        <xsl:call-template name="CreateServerFolderPathForApplication">
          <xsl:with-param name="application" select="$application" />
          <xsl:with-param name="environment" select="$EnvironmentTarget" />
          <xsl:with-param name="folderPath" select="$shareServer" />
        </xsl:call-template>
      </xsl:attribute>
    </xsl:if>
  </xsl:template>

  <!-- Begin : Function to Create a folder Path -->
  <xsl:template name="CreateServerFolderPathForApplication">
    <xsl:param name="folderPath" />
    <xsl:param name="application" />
    <xsl:param name="environment" />
    <xsl:param name="serverName" />


    <!-- String Folder Path\Application Name\Vversion-->
    <xsl:variable name="returnPath">
      <xsl:call-template name="EnsurePathEndTerminator">
        <xsl:with-param name="value" select="$folderPath" />
      </xsl:call-template>
      <xsl:value-of select="$environment" />\<xsl:value-of select="$application/@name"/>\V<xsl:value-of select="$application/@version"/>
    </xsl:variable>

    <!-- ensure last slash-->
    <xsl:call-template name="EnsurePathEndTerminator">
      <xsl:with-param name="value" select="$returnPath" />
    </xsl:call-template>
  </xsl:template>
  <!-- End : Function to Create a folder Path -->

  <!-- Begin : Function to ensure a separator at the end of a value -->
  <xsl:template name="EnsurePathEndTerminator">
    <xsl:param name="value" />
    <xsl:param name="separator" select="'\'" />

    <xsl:choose>
      <xsl:when test="$separator= substring($value, string-length($value) - string-length($separator) +1)">
        <xsl:value-of select="$value"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:variable name="returnValue">
          <xsl:value-of select="$value"/>
          <xsl:value-of select="$separator"/>
        </xsl:variable>
        <xsl:value-of select="$returnValue"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!-- End : Function to ensure a separator at the end of a value -->



</xsl:stylesheet>
